import requests
import logging
from .._register import register_command
from ..ba.ba_url import BA_url_list


@register_command("/攻略", "国服千里眼", "国际服千里眼", "国际服人权")
async def ba_command(client):
    msg = client.msg
    url_ex = ""
    if msg.startswith("/攻略"):
        if msg == "/攻略":
            await client.reply(
                "\n使用方法:/攻略 名称 或 /攻略 关卡\n例：/攻略 未花 /攻略 H24-1"
            )
            return True
        if "-" in msg:  # 推图
            stage = msg.split("攻略")[1].strip().upper()
            url_ex = f"{stage}.png"
        else:
            url = "https://tutorial.arona.diyigemt.com/api/v2/image"
            name = msg.split("攻略")[1].strip().upper()
            params = {"name": name, "method": 3}
            response = requests.get(url, params=params)
            if response.status_code == 200:
                data = response.json()
                if data.get("code") == 200 and data.get("data"):
                    content = data["data"][0]["content"]
                    url = "https://arona.cdn.diyigemt.com/image" + content
                    await client.reply(img_url=url)
                else:
                    await client.reply(content="没有找到结果，是否输入错误")
            else:
                await client.reply(content="请求失败，发生错误")
            return True

    url = BA_url_list[msg.split()[0]] + url_ex
    try:
        await client.reply(img_url=url)
    except Exception as e:
        logging.warning(e)
        await client.reply(content="没有找到结果，是否输入错误")
    return True
